package com.example.edu_ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.commonutils.JwtUtils;
import com.example.commonutils.MD5;
import com.example.edu_ucenter.entity.UcenterMember;
import com.example.edu_ucenter.entity.vo.UserRegister;
import com.example.edu_ucenter.mapper.UcenterMemberMapper;
import com.example.edu_ucenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.servicebase.exceptionhandler.ZDYException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-09-15
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public String login(UcenterMember member) {

        String mobile = member.getMobile();
        String password = member.getPassword();



        System.out.println(StringUtils.isEmpty(mobile));
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new ZDYException(20001,"登录失败");
        }

        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember user = baseMapper.selectOne(wrapper);
        if (user == null) {
            throw new ZDYException(20001,"登录失败");
        }


        if (!MD5.encrypt(password).equals(user.getPassword())){
            throw new ZDYException(20001,"登录失败");
        }

        if (user.getIsDisabled()){
            throw new ZDYException(20001,"登录失败");
        }

        String token = JwtUtils.getJwtToken(user.getId(), user.getNickname());

        return token;
    }

    @Override
    public void register(UserRegister register) {

        String code = register.getCode();
        String mobile = register.getMobile();
        String nickname = register.getNickname();
        String password = register.getPassword();

        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(redisCode)){
            throw new ZDYException(20001,"注册失败");
        }

        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if (count > 0){
            throw new ZDYException(20001,"注册失败");
        }
        UcenterMember user = new UcenterMember();
        user.setMobile(mobile);
        user.setNickname(nickname);
        user.setPassword(password);
        user.setIsDisabled(false);
        user.setAvatar("");
        baseMapper.insert(user);

    }

    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    @Override
    public Integer countRegisterDay(String day) {
        return baseMapper.countRegisterDay(day);
    }

}
